﻿using HelperFromTheSkeleton.HttpManage;
using HelperFromTheSkeleton.Model.PageModel;
using HelperFromTheSkeleton.Model.RequestModels;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace HelperFromTheSkeleton.WPFHttpRepository
{
    public class BaseHttpRepository
    {
        private readonly IHttpService _httpService;
        private readonly Dictionary<string, string> _baseHead;

        public BaseHttpRepository(IHttpService httpService)
        {
            _httpService = httpService;
            _baseHead = new Dictionary<string, string>();
        }

        public async Task<ResultT?> PostAsync<RequestT, ResultT>(RequestT resultT, string url, string? token = null) where RequestT : class, new()
        {
            ResultModel<ResultT>? result;
            if (!string.IsNullOrWhiteSpace(token))
            {
                _baseHead.TryAdd("Authorization", "Bearer " + token);
            }
            if (_baseHead.Count >= 1)
            {
                result = await _httpService.PostAsync<ResultModel<ResultT>, RequestT>(_baseHead, resultT, url);
            }
            else
            {
                result = await _httpService.PostAsync<ResultModel<ResultT>, RequestT>(resultT, url);
            }
            if (result == null) return default;
            if (result.IsSucceed == false)
            {
                throw new InvalidOperationException($"内部请求失败：{result.Message}");
            };
            return result.Data;
        }

        public async Task<(List<ResultT>, PageResultModel)> PagePostAsync<RequestT, ResultT>(RequestT resultT, string url, string? token = null) where RequestT : class, new()
        {
            PageResultModel<ResultT>? result;
            if (!string.IsNullOrWhiteSpace(token))
            {
                _baseHead.TryAdd("Authorization", "Bearer " + token);
            }
            if (_baseHead.Count >= 1)
            {
                result = await _httpService.PostAsync<PageResultModel<ResultT>, RequestT>(_baseHead, resultT, url);
            }
            else
            {
                result = await _httpService.PostAsync<PageResultModel<ResultT>, RequestT>(resultT, url);
            }
            if (result == null) return default;
            if (result.IsSucceed == false)
            {
                throw new InvalidOperationException($"内部请求失败：{result.Message}");
            };
            return (result.Data, result.PageData);
        }

        public async Task PostAsync<RequestT>(RequestT resultT, string url, string? token = null) where RequestT : class, new()
        {
            ResultModel? result;
            if (!string.IsNullOrWhiteSpace(token))
            {
                _baseHead.TryAdd("Authorization", "Bearer " + token);
            }
            if (_baseHead.Count >= 1)
            {
                result = await _httpService.PostAsync<ResultModel, RequestT>(_baseHead, resultT, url);
            }
            else
            {
                result = await _httpService.PostAsync<ResultModel, RequestT>(resultT, url);
            }
            if (result == null) throw new InvalidOperationException("内部请求失败:无效的请求结果");
            if (result.IsSucceed == false)
            {
                throw new InvalidOperationException($"内部请求失败：{result.Message}");
            };
            return;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="ResultT"></typeparam>
        /// <param name="date"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public async Task<ResultT?> GetAsync<ResultT>(Dictionary<string, object> date, string url, string? token = null)
        {
            ResultModel<ResultT>? result;
            if (!string.IsNullOrWhiteSpace(token))
            {
                _baseHead.TryAdd("Authorization", "Bearer " + token);
            }
            if (_baseHead.Count >= 1)
            {
                result = await _httpService.GetAsync<ResultModel<ResultT>>(_baseHead, date, url);
            }
            else
            {
                result = await _httpService.GetAsync<ResultModel<ResultT>>(date, url);
            }
            if (result == null) return default;
            if (result.IsSucceed == false)
            {
                throw new InvalidOperationException($"内部请求失败：{result.Message}");
            };
            return result.Data;
        }
    }
}